package com.qingke.easyjava.jdbc.command.impl;

import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.PlayerCommand;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.pojo.Question;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class AskCommand extends PlayerCommand {	//PlayerCommand实现command

    public AskCommand(Player player) {
        super(player);
    }

    @Override
    public void executePlayerCommand() {		//执行代码  PlayerCommand 
    	
        String content = QingkeConsole.askUserInput("请输入你的问题");
        int credit = QingkeConsole.askUserInputInt("请输入问题的奖励分值", false);	//一、
        
        if (credit > player.getScore()) {
            credit = player.getScore();
            QingkeConsole.println("输入分值超过当前用户分数！默认使用用户分数为奖励值");
        }
        
        Question question = new Question(-1, content, player, credit);				//long id，问题的ID,提问者，分值。
        new BeSuperDao().upsertQuestion(question);
        
        player.getQuestions().add(question);
        player.setScore(player.getScore() - credit);

        QingkeConsole.println("你的提问已经被记录！");
    }

}
//一、
/*    public static int askUserInputInt(String prompt) {
        
        int result = -1;
        while (true) {
            String out = askUserInput(prompt);
            
            try {
                result = Integer.parseInt(out);
            } catch (Exception e) {
                println("请输入合法的数字！");
            }

            if (result != -1) {
                break;
            }
        }

        return result;
    }

    public static int askUserInputInt(String prompt, boolean isRequired) {

        if (isRequired) {
            return askUserInputInt(prompt);//判断是否合法。
        }

        try {
            System.out.print(prompt + ": ");

            String str = scanner.nextLine();
            return Integer.parseInt(str);		//将字符串转化为数值、
        } catch (Exception e) {
            println("不合法的数字！默认使用0");
        }

        return 0;
    }*/
//二、
/*    public void upsertQuestion(Question question) {
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        String sqlInsert = "INSERT INTO `player`.`question` (`value`, `credit`, `id`, `isOpen`) VALUES (?,?,?,?)";
        String sqlUpdate = "UPDATE `player`.`question` SET `value`=?, `credit`=?, `id`=?, `isOpen`=? WHERE `id`=?";
        boolean isUpdate = question.getId() != -1;
        try {
            conn = getConnection();
            
            String sql = sqlInsert;
            if (isUpdate) {
                sql = sqlUpdate;
            }
            statement = conn.prepareStatement(sql);
            
            statement.setString(1, question.getValue());
            statement.setInt(2, question.getCredit());
            statement.setLong(3, question.getFrom().getId());
            statement.setString(4, question.isOpen() ? "Y" : "N");
            if (isUpdate) {
                statement.setLong(5, question.getId());
            }
            
            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            cleanup(conn, statement, rs);
        }
    }*/